package com.abbas.CayTcore.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentStatePagerAdapter
import com.abbas.CayTcore.base.BaseFragment
import com.abbas.CayTcore.databinding.FragmnetWindingUpBinding
import com.abbas.CayTcore.entity.LW
import com.abbas.CayTcore.presenter.WindingAllDataUpPresenter
import com.abbas.CayTcore.startActivity
import com.abbas.CayTcore.view.ViewContract
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import q.rorbin.verticaltablayout.adapter.TabAdapter

/**
 * 绕线主页
 */
class WindingAllDataUpFragment :
    BaseFragment<ViewContract.WindingAllDataUpView, WindingAllDataUpPresenter, FragmnetWindingUpBinding>(),
    ViewContract.WindingAllDataUpView {

    override fun initPresenter(): WindingAllDataUpPresenter = WindingAllDataUpPresenter()

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): FragmnetWindingUpBinding {
        return FragmnetWindingUpBinding.inflate(inflater, container, false)
    }

    override fun initData() {
        getAllCore()
    }

    override fun initEvent() {
        binding.addBt.setOnClickListener {
            startActivity(AddCoreTypeActivity::class.java)
        }
        binding.tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(p0: TabLayout.Tab?) {
                presenter.groupCore(p0?.tag as LW)
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {

            }

            override fun onTabReselected(p0: TabLayout.Tab?) {

            }

        })
    }

    override fun getAllCore() {
        presenter.getAllCore()
    }

    override fun onCoreLWGet(data: List<String>) {
        data.forEach {
            binding.tabLayout.apply {
                addTab(newTab().apply {
                    text = it + "系列"
                    tag = LW(
                        it.substring(0..1).toInt(),
                        it.substring(2..3).toInt()
                    )
                })
            }
        }
    }

    override fun coreLwTypeFilter(adapter: TabAdapter, pagerAdapter: FragmentStatePagerAdapter) {
        binding.apply {
            vp.adapter = pagerAdapter
            verticalTabLayout.apply {
                setupWithViewPager(binding.vp)
                setTabAdapter(adapter)
            }
        }
    }


}